//
//  VIZSecarchView.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/7.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit

class VIZSecarchView: UIView {

    @IBOutlet weak var rightConst: NSLayoutConstraint!
    @IBOutlet weak var textFiel: UITextField!
    
    @IBOutlet weak var cancleBtn: UIButton!
    
    @IBAction func beginEdit(sender: AnyObject) {
        //设置右边距减按钮宽度
        self.rightConst.constant = self.cancleBtn.frame.width
        //设置动画
        UIView.animateWithDuration(0.25) { () -> Void in
        self.layoutIfNeeded()
            
        }
        
        
    }
    
    override func awakeFromNib() {
        //设置大小
        self.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: self.bounds.height)
        
        //设置图片
        self.textFiel.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        self.textFiel.leftView?.frame = CGRect(x: 0, y: 0, width: self.bounds.height, height: self.bounds.height)
        //设置图片显示格式
        self.textFiel.leftView?.contentMode = .Center
        //设置为始终显示
        self.textFiel.leftViewMode = .Always
        
        cancleBtn.addTarget(self, action: "cancleBtnClick", forControlEvents: .TouchUpInside)
//        self.textFiel.addTarget(self, action: "textFielClick", forControlEvents: .TouchUpInside)
//        print("1111")
    }
    
    func cancleBtnClick() {
        
        //取消textFiel的第一响应者
        textFiel.resignFirstResponder()
        rightConst.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.layoutIfNeeded()
        }
        
    }
    
    //创建类方法来返回头视图
    class func loadSearchView () -> VIZSecarchView {
        
        let view = NSBundle.mainBundle().loadNibNamed("SearchView", owner: nil, options: nil).last as! VIZSecarchView
        
        return view
        
    }
    
    @IBAction func cancleBtnClick(sender: UIButton) {
        
        
        
    }
}
